'use strict';

import { version } from 'moment';



class CsArenaDao {
    handle: any
    constructor(db) {
        this.handle = db.collection("csArena")
        this.handle.createIndex("pID1", 1)
        this.handle.createIndex("pID2", 1)
        this.handle.createIndex("time", 1)
        this.handle.createIndex({ "pID1": 1, "time": -1 })
    }

    newAndSave = async (name1, name2, pID1, pID2, player1, player2, lvl1, lvl2, server1, server2, result, seed, version): Promise<any> => {
        let date = new Date().getTime()
        return await this.handle.insertOne({
            name1: name1,
            name2: name2,
            pID1: pID1,
            pID2: pID2,
            player1: player1,
            player2: player2,
            lvl1: lvl1,
            lvl2: lvl2,
            server1:server1,
            server2:server2,
            result: result,
            version: version,
            seed: seed,
            time: date,
        });
    }

    findOne = async (query): Promise<any> => {
        return await this.handle.findOne(query)
    }

    find = async (query): Promise<any[]> => {
        return await this.handle.find(query).toArray()
    }

    findByPage = async (query, start: number, size: number): Promise<any[]> => {
        return await this.handle.find(query).skip(start).limit(size).toArray()
    }

    findByPageAndSort = async (query, sort, start, size): Promise<any[]> => {
        return await this.handle.find(query).sort(sort).skip(start).limit(size).toArray()
    }
    //opts  { multi: true}
    updateMany = async (query, data): Promise<any> => {
        return await this.handle.updateMany(query, data);
    }

    updateOne = async (query, data): Promise<any> => {
        return await this.handle.updateOne(query, data);
    }
    count = async (query): Promise<number> => {
        return await this.handle.countDocuments(query)
    }

    remove = async (query): Promise<any> => {
        return await this.handle.remove(query, {
            multi: true
        });
    }
}

export default CsArenaDao